﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using ProjectVLIB.VOL;
using System.Data;
using ProjectVLIB.DAL;

namespace ProjectVLIB.BLL
{
    class HoaDonBanBL
    {
        private DataAccess dataAccess;
        public HoaDonBanBL()
        {
            dataAccess = new DataAccess();
        }

        //Thêm
        public bool insertHoaDonBan(HoaDonBan obj)
        {
            SqlParameter[] param = new SqlParameter[5];
            param[0] = new SqlParameter("@MaKhach", SqlDbType.Int) { Value = obj.MaKhach };
            param[1] = new SqlParameter("@NgHD", SqlDbType.DateTime) { Value = obj.NgHD };
            param[2] = new SqlParameter("@DienGiai", SqlDbType.VarChar) { Value = obj.DienGiai };
            param[3] = new SqlParameter("@Duyet", SqlDbType.Bit) { Value = obj.Duyet };
            param[4] = new SqlParameter("@NgDuyet", SqlDbType.DateTime) { Value = obj.NgDuyet };

            string strSql = "INSERT INTO tbl_HoaDonBan(TenHang,HinhAnh,DVT,DonGia,MaNhaCungCap,MaLoaiHang,MoTa) VALUES (@TenHang,@HinhAnh,@DVT,@DonGia,@MaNhaCungCap,@MaLoaiHang,@MoTa)";
            dataAccess.Execute(strSql);

            return true;
        }

        //Xóa
        public bool deleteHoaDonBan(HoaDonBan obj)
        {
            SqlParameter[] param = new SqlParameter[1];
            param[0] = new SqlParameter("@MaHD", SqlDbType.Int) { Value = obj.MaHD };

            string strSql = "DELETE FROM tbl_HoaDonBan WHERE MaHD = @MaHD";
            dataAccess.Execute(strSql);
            return true;
        }

        //Sửa
        public bool updateHoaDonBan(HoaDonBan obj)
        {
            SqlParameter[] param = new SqlParameter[6];
            param[0] = new SqlParameter("@MaKhach", SqlDbType.Int) { Value = obj.MaKhach };
            param[1] = new SqlParameter("@NgHD", SqlDbType.DateTime) { Value = obj.NgHD };
            param[2] = new SqlParameter("@DienGiai", SqlDbType.VarChar) { Value = obj.DienGiai };
            param[3] = new SqlParameter("@Duyet", SqlDbType.Bit) { Value = obj.Duyet };
            param[4] = new SqlParameter("@NgDuyet", SqlDbType.DateTime) { Value = obj.NgDuyet };
            param[5] = new SqlParameter("@MaHD", SqlDbType.Int) { Value = obj.MaHD };

            string strSql = "UPDATE tbl_HoaDonBan SET " +
                            "MaKhach = @MaKhach, " +
                            "NgHD = @NgHD, " +
                            "DienGiai = @DienGiai, " +
                            "Duyet = @Duyet " +
                            "NgDuyet = @NgDuyet " +
                            "WHERE MaHD = @MaHD";
            dataAccess.Execute(strSql);
            return true;
        }

        //Tìm
        public DataTable searchIDHoaDonBan(HoaDonBan obj)
        {
            SqlParameter[] param = new SqlParameter[1];
            param[0] = new SqlParameter("@MaHD", SqlDbType.Int) { Value = obj.MaHD };

            string strSql = "SELECT * FROM tbl_HoaDonBan WHERE MaHD = @MaHD";
            return dataAccess.GetData(strSql, param);
        }
    }
}
